<?php

namespace app\index\controller;
use think\Controller;
use think\Db;
// use app\index\model\Login as LoginModel;
header("Access-Control-Allow-Origin: *");//跨域访问头部
/**
* 首页数据接口
*/
class Index extends Controller
{
	public function index(){
		$phone=input('user');//接收手机号
		$num=input('data');//接收数字
		$id=db('home')->field('id')->where(array('moblie'=>$phone))->find();//用户id
		// $id=2;// 用户id
		$merchantId=Db::table('app_merchant')->field('id')->where(array('home_id'=>$id['id']))->find();//商户详细信息表id
		$confId=Db::table('app_conf')->field('mchId')->where(array('mid'=>$merchantId['id']))->find();//商户配置表id
		$mchId=Db::table('app_menoy')->field('sum(total_fee),shijian')->where(array('mch_id'=>$confId['mchId']))->group('mch_id')->where(array('status_jy'=>0))->find();//交易信息,按商户编号分组求和，交易总金额
		$total=(int)$mchId['sum(total_fee)']/(int)100;//总金额(元)
		$count=Db::table('app_menoy')->where(array('mch_id'=>$confId['mchId']))->count();//总交易单数
		$todayend = strtotime(date('Y-m-d'.'00:00:00',time()+3600*24));//第二天最早时间
        $todaystart = strtotime(date('Y-m-d'.'00:00:00',time()));//当天最早时间
        $where = "shijian >= ".$todaystart." and shijian < ".$todayend.""; 
        $dayRes=Db::table('app_menoy')->field('sum(total_fee),shijian')->where(array('mch_id'=>$confId['mchId']))->where($where)->where(array('status_jy'=>0))->group('mch_id')->find();//交易信息,按商户编号分组求和，今日交易总金额
		$dayTotal=(int)$mchId['sum(total_fee)']/(int)100;//今日总金额(元)
		$dayCount=Db::table('app_menoy')->where(array('mch_id'=>$confId['mchId']))->where($where)->count();//今日交易单数
		$thumb=Db::table('app_merchant')->field('thumb,name')->where(array('home_id'=>$id['id']))->find();//商户收款码图片地址和商户名称
		$order=Db::table('app_menoy')->field('id,out_trade_no,total_fee,shijian,body')->where(array('mch_id'=>$confId['mchId']))->where(array('status_jy'=>0))->select();//本商户下的所有交易订单号
		
		$data=array(
			'total'=>$total,//总金额
			'order'=>$count,//总交易单
			'dayTotal'=>$dayTotal,//今日交易金额
			'src'=>$thumb['thumb']//二维码src
			);
		if ($num==1) {
			echo $dayTotal;exit;
		}elseif ($num==2) {
			echo $total;exit;
		}elseif ($num==3) {
			echo $count;exit;
		}elseif ($num==4) {
			echo $thumb['thumb'];exit;
		}elseif ($num==5) {
			echo $thumb['name'];exit;
		}elseif ($num==6) {
			echo $dayCount;exit;
		}else{
			return json_encode($order);
		}
		// return json_encode($data);//json数据反给前台
		// http://47.93.55.230:86/public/index.php/index/index/index?user=15124737674
		// {"total":0.02,"order":2,"dayTotal":0.02,"src":"http:\/\/pan.baidu.com\/share\/qrcode?w=350&h=350&url=http:\/\/wzzf.nmejiefu.com\/miusi.php?sh_id=1"}

	}
	// 版本号
	public function edition(){
		$editRes=db('edition')->select();
		return json_encode($editRes);

	}
	// 最近7天的交易
	public function weekDay(){
		$phone=input('user');//接收手机号
		$num=input('data');//接收数字
		$id=db('home')->field('id')->where(array('moblie'=>$phone))->find();//用户id
		// $id=2;// 用户id
		$merchantId=Db::table('app_merchant')->field('id')->where(array('home_id'=>$id['id']))->find();//商户详细信息表id
		$confId=Db::table('app_conf')->field('mchId')->where(array('mid'=>$merchantId['id']))->find();//商户配置表id
		$mchId=Db::table('app_menoy')->field('sum(total_fee),shijian')->where(array('mch_id'=>$confId['mchId']))->group('mch_id')->find();//交易信息,按商户编号分组求和，交易总金额
		$total=(int)$mchId['sum(total_fee)']/(int)100;//总金额(元)
		$count=Db::table('app_menoy')->where(array('mch_id'=>$confId['mchId']))->count();//总交易单数
		$start= strtotime(date('Y-m-d', strtotime('-7 days')));//前7天的时时间戳
		$end=time();//当前时间
		$where = "shijian >= ".$start." and shijian < ".$end.""; 
        $dayRes=Db::table('app_menoy')->field('sum(total_fee),shijian')->where(array('mch_id'=>$confId['mchId']))->where($where)->where(array('status_jy'=>0))->group('mch_id')->find();//交易信息,按商户编号分组求和，近7日交易总金额
        $sevenTotal=(int)$dayRes[ 'sum(total_fee)']/(int)100;//近7日交易总金额
		$dayCount=Db::table('app_menoy')->where(array('mch_id'=>$confId['mchId']))->where($where)->where(array('status_jy'=>0))->count();//近7日交易单数
		$thumb=Db::table('app_merchant')->field('thumb')->where(array('home_id'=>$id['id']))->find();//商户收款码图片地址
		$day7=date('Y-m-d H:i:s', strtotime('-7 days'));//前7天的时间
		$day1=date('Y-m-d H:i:s');//当前时间

		$kaiq = strtotime(date('Y-m-d 00:00:00',time()-3600*144));//条件最早时间

        $jieq = strtotime(date('Y-m-d 23:59:59',time()));//条件最晚时间

        $w= "shijian >= ".$kaiq." and shijian <= ".$jieq;
		$daysRes=Db::table('app_menoy')->field('sum(total_fee)/100 as su,shijian')->order('id desc')->where(array('mch_id'=>$confId['mchId']))->where($w)->where(array('status_jy'=>0))->group('shijian')->limit(5)->select();//交易信息,按交易时间分组 求和，近7日每天的交易总金额
		$arr=array();
		foreach ($daysRes as $k => $v) {
			$arr[$k]['total']=$v['su'];
			$arr[$k]['time']=date('m/d',$v['shijian']);
		
		}
		$array = array();
		foreach($arr as $v){
		    if(!isset($array[$v['time']])) $array[$v['time']] = $v;
		    else $array[$v['time']]['total'] += $v['total'];
		}
		$attr=array();
		foreach ($array as $k => $v) {
			$attr[]=$v;
		}
		if ($num==1) {
			echo $sevenTotal;exit;//近7日交易总金额
		}elseif ($num==2) {
			echo $dayCount;exit;//近7日交易单数
		}elseif ($num==3) {
			echo $day7;//前7天的时间
		}elseif ($num==4) {
			echo $day1;//当天时间
		}else{
			 return json_encode($attr);//近7日每天的交易总金额和时间
		}
		// $arr=array();
		// $arr=getdate();//本周
		// $num=$arr['wday'];//本周星期几
		// $start=time()-($num-1)*24*60*60;
		// $end=time()+(7-$num)*24*60*60;
		// select * from `app_merchant` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);近7天记录
	}
	// 商户资料
	public function merchantRes(){
		$phone=input('user');//接收手机号
		$id=db('home')->where(array('moblie'=>$phone))->find();//用户id
		$merchantRes=Db::table('app_merchant')->field('id,name,type,category,contacts,mobile,door,merchant_name,locat,address,license_number,bnusiness,period,business_license,legal_person,number,photo_positive,photo_negative,service_agreement,account_type,opening_bank,name_bank,bank_account,thumb,head_thumb,status')->where(array('home_id'=>$id['id']))->where(array('status'=>1))->find();//商户资料
		return json_encode($merchantRes);
	}
	// 帮助中心
	public function article(){
		$artRes=db('conte')->field('id,title,conte')->select();
		return json_encode($artRes);
	}
}